﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.ErrorLogging;

namespace Sinacor.Infra.Common.ErrorLogging
{
    public abstract class SinacorEnvironmentCollector : EnvironmentCollector
    {
        public override PSE.Framework.ErrorLogging.EnvironmentData Collect(Dictionary<string, object> extensionData)
        {
            EnvironmentData environmentData = base.Collect(extensionData) as EnvironmentData;

            if (environmentData != null &&
                environmentData is DbEnvironmentData)
            {
                DbEnvironmentData dbEnvironmentData = (DbEnvironmentData)environmentData;

                try
                {
                    if (extensionData != null)
                    {
                        if (extensionData.Keys.Any(k => k.StartsWith("Db")))
                        {
                            dbEnvironmentData.DbEnvironment = dbEnvironmentData.DbEnvironment ?? new DbEnvironment();
                            dbEnvironmentData.DbEnvironment.DataSource = this.GetData<string>(() => (string)Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\SINACOR\Infra.Data\Instances", "Default", string.Empty), "DbEnvironment.DataSource", environmentData);
                        }

                        if (extensionData.ContainsKey("DbUserId"))
                        {
                            dbEnvironmentData.DbEnvironment = dbEnvironmentData.DbEnvironment ?? new DbEnvironment();
                            dbEnvironmentData.DbEnvironment.UserId = this.GetData<string>(() => (string)extensionData["DbUserId"], "DbEnvironment.UserId", environmentData);
                        }

                        if (extensionData.ContainsKey("DbCommandType"))
                        {
                            dbEnvironmentData.DbEnvironment = dbEnvironmentData.DbEnvironment ?? new DbEnvironment();
                            dbEnvironmentData.DbEnvironment.CommandType = this.GetData<string>(() => (string)extensionData["DbCommandType"], "DbEnvironment.CommandType", environmentData);
                        }

                        if (extensionData.ContainsKey("DbCommandText"))
                        {
                            dbEnvironmentData.DbEnvironment = dbEnvironmentData.DbEnvironment ?? new DbEnvironment();
                            dbEnvironmentData.DbEnvironment.CommandText = this.GetData<string>(() => (string)extensionData["DbCommandText"], "DbEnvironment.CommandText", environmentData);
                        }

                        if (extensionData.ContainsKey("DbParameters"))
                        {
                            dbEnvironmentData.DbEnvironment = dbEnvironmentData.DbEnvironment ?? new DbEnvironment();
                            dbEnvironmentData.DbEnvironment.Parameters = this.GetData<List<string>>(() => (List<string>)extensionData["DbParameters"], "DbEnvironment.Parameters", environmentData);
                        }
                    }
                }
                catch (Exception ex)
                {
                    dbEnvironmentData = (DbEnvironmentData)HandleException(dbEnvironmentData, ex);
                }
            }

            return environmentData;
        }
    }
}
